package com.itheima.bakery.demos.web.controller;

import com.itheima.bakery.demos.web.model.Order;
import com.itheima.bakery.demos.web.model.User;
import com.itheima.bakery.demos.web.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@RestController
public class CookieController {

    @GetMapping("/api/get-user-id")
    public String getUserId(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Optional<String> userId = Arrays.stream(cookies)
                    .filter(cookie -> "user_id".equals(cookie.getName()))
                    .map(Cookie::getValue)
                    .findFirst();
            return userId.orElse("not_logged_in");
        }
        return "not_logged_in";
    }

}